package com.sir.tao.carhire.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.UserEntity;
import com.sir.tao.carhire.service.UserService;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author tao
 * @data 2021/12/6 10:48
 * @description: 管理员登录接口
 */
@RestController
@Api(tags = "用户业务接口")
@RequestMapping("/userBusiness")
public class UserBusinessController {

    @Resource
    private UserService userService;
    /**
     * 用户登录接口
     * @param userEntity 传的账号密码
     * @return 成功返回token
     */
    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    @ApiImplicitParam(name = "用户登录接口", value = "用户登录接口")
    public CommonResult<?> login(@RequestBody UserEntity userEntity){

        UserEntity trueUser = userService.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUserName,userEntity.getUserName())
        );
        boolean b = !Objects.isNull(trueUser) && trueUser.getPassword().equals(userEntity.getPassword());
        //账号密码正确
        if (b){
            return CommonResult.success(trueUser);
        }else {
            return CommonResult.fail("账号密码错误");
        }
    }

    /**
     * 用户注册接口
     * @param userEntity 用户注册接口
     * @return 用户注册接口
     */
    @PostMapping("/register")
    @ApiOperation("用户注册接口")
    @ApiImplicitParam(name = "用户注册接口", value = "用户注册接口")
    public CommonResult<?> register(@RequestBody UserEntity userEntity){
        if (StringUtils.isEmpty(userEntity.getPhone())){
            return CommonResult.fail("请输入手机号");
        }
        UserEntity trueUser = userService.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUserName,userEntity.getUserName())
        );
        if (Objects.nonNull(trueUser)){
            return CommonResult.fail("该用户已存在");
        }
        boolean i = userService.save(userEntity);
        if(i){
            return CommonResult.success("注册成功");
        }else {
            return CommonResult.fail("注册失败");
        }
    }


}
